<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:v="livcos.org/ns/view"
  exclude-result-prefixes="doc v">

	<xsl:template match="doc:table">
    <div id="{@id}" class="block table">
    	<v:style ref="{resolve-uri('styles.css')}"/>
    	<table border="1" cellpadding="4" cellspacing="0">
        <xsl:if test="not(doc:head) and (normalize-space(doc:title) != '')">
        	<thead>
	          <tr>
	            <th colspan="{count(doc:body/doc:row/doc:cell)}">
								<span class="inlineEdit">
									<span class="inlnEd_path" title="title"/>
									<xsl:value-of select="doc:title"/>
								</span>
	            </th>
	          </tr>
          </thead>
        </xsl:if>
        <xsl:apply-templates select="doc:head"/>
        <xsl:apply-templates select="doc:body"/>
        <xsl:apply-templates select="doc:foot"/>
      </table>
      <xsl:if test="normalize-space(doc:note) != ''">
      	<p class="table_note">
      		<span class="inlineEdit">
      			<span class="inlnEd_path" title="note"/>
      			<xsl:apply-templates select="doc:note/node()"/>
      		</span>
      	</p>
      </xsl:if>
    </div>
  </xsl:template>
  
	<xsl:template match="doc:table/doc:head">
		<thead>
			<xsl:if test="normalize-space(../doc:title) != ''">
				<tr>
					<th colspan="{count(../doc:body/doc:row/doc:cell)}">
						<span class="inlineEdit">
							<span class="inlnEd_path" title="title"/>
							<xsl:value-of select="../doc:title"/>
						</span>
					</th>
				</tr>
			</xsl:if>
			<xsl:apply-templates/>
		</thead>
	</xsl:template>
	
	<xsl:template match="doc:table/doc:body | doc:table/doc:foot">
		<tbody>
			<xsl:apply-templates/>
		</tbody>
	</xsl:template>
	
	<xsl:template match="doc:row">
		<tr id="{@id}">
			<xsl:apply-templates/>
		</tr>
	</xsl:template>
	
	<xsl:template match="doc:table/doc:head/doc:row/doc:cell">
    <th id="{@id}">
      <xsl:if test="@colspan &gt; 1">
        <xsl:attribute name="colspan">
          <xsl:value-of select="@colspan" />
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="@rowspan &gt; 1">
        <xsl:attribute name="rowspan">
          <xsl:value-of select="@rowspan" />
        </xsl:attribute>
      </xsl:if>
    	<xsl:apply-templates select="." mode="content"/>
    </th>
  </xsl:template>

	<xsl:template match="doc:table/doc:body/doc:row/doc:cell">
    <td id="{@id}">
      <xsl:if test="@colspan &gt; 1">
        <xsl:attribute name="colspan">
          <xsl:value-of select="@colspan" />
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="@rowspan &gt; 1">
        <xsl:attribute name="rowspan">
          <xsl:value-of select="@rowspan" />
        </xsl:attribute>
      </xsl:if>
    	<xsl:apply-templates select="." mode="content"/>
    </td>
  </xsl:template>

	<xsl:template match="doc:table/doc:foot/doc:row/doc:cell">
    <td id="{@id}" style="font-style: italic;">
      <xsl:if test="@colspan &gt; 1">
        <xsl:attribute name="colspan">
          <xsl:value-of select="@colspan" />
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="@rowspan &gt; 1">
        <xsl:attribute name="rowspan">
          <xsl:value-of select="@rowspan" />
        </xsl:attribute>
      </xsl:if>
      <xsl:apply-templates select="." mode="content"/>
    </td>
	</xsl:template>
	
	<xsl:template match="doc:cell" mode="content">
    <xsl:choose>
      <xsl:when test="*[@id]">
        <xsl:apply-templates/>
      </xsl:when>
      <xsl:otherwise>
        <span class="inlineEdit">
          <xsl:apply-templates/>
        </span>
      </xsl:otherwise>
    </xsl:choose>
	</xsl:template>
	
</xsl:stylesheet>
